﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
using com.lover.common;
using System.IO;
using com.lover.astd.common.model;
using com.lover.common.http;

namespace com.lover.astd.common.partner.impl
{
    public class WebxgameLogin : LoginImplBase
    {
        public WebxgameLogin() : base() { }
        public override LoginResult login(ref List<Cookie> cookies, string verify_code = null, string extra = null)
        {
            LoginResult result = new LoginResult();
            logging();
            string loginurl = "http://www.webxgame.com/interface/home/login.php";
            string data = String.Format("username={0}&password={1}&button.x=38&button.y=23&tempurl=http%3A%2F%2Ftd.webxgame.com%2Fservermorelist.php&sitename=astd", _username, _password);
            HttpResult resp = TransferMgr.doPostPure(loginurl, data, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInLogin; return result; }

            string retstr = resp.getContent();

            if (retstr.IndexOf("alert") >= 0)
            {
                result.StatusCode = LoginStatusCode.FailInLogin;
                return result;
            }

            string redirecturl = String.Format("http://td.webxgame.com/openserver.php?serverid={0}", _acc.ServerId);

            resp = TransferMgr.doGetPure(redirecturl, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInGotoGameUrl; return result; }
            string content = resp.getContent();
            //<script type="text/javascript">window.location.href = "http://s54.as.aoshitang.com/root/start.action?accid=4331571&accname=doomsword&serverid=54&tstamp=1390450397356&ticket=05e88e6e466ec1c4b5634884f4fd072c&fcm=0&mixflag=uoyoo";</script>
            Match match = Regex.Match(content, ".*?top\\.window\\.location.*?=.*?[\"']+([^\"']*?)[\"']+.*");
            if (match == null || match.Groups == null || match.Groups.Count < 2)
            {
                result.StatusCode = LoginStatusCode.FailInGotoGameUrl; return result;
            }
            string starturl = match.Groups[1].Value;

            processStartGame(starturl, result, ref cookies);
            return result;
        }
    }
}
